package de.xXephis.MeineMensa;

import java.io.File;
import java.io.FilenameFilter;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.widget.Toast;


public class Preferences extends PreferenceActivity implements OnPreferenceClickListener{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.preferences);
		findPreference("clearcache").setOnPreferenceClickListener(this);
	}

	public boolean onPreferenceClick(Preference preference) {
		if("clearcache".equals(preference.getKey()))
		{
			clearCache();
		}
		
		return false;
	}
	
	private void clearCache()
	{
		clearImgCache();
		clearXMLCache();
		Toast.makeText(this, "Cache bereinigt.", 1000).show();
	}
	
	private void clearXMLCache()
	{
		SharedPreferences sp=getSharedPreferences("EatData", Context.MODE_PRIVATE);
		SharedPreferences.Editor spe=sp.edit();
		spe.clear();
		spe.commit();
		Log.d("cacheClearer", "XML Cache cleared.");
	}
	
	private void clearImgCache()
	{
		if(!isSDWritable())
		{
			Toast.makeText(this, "SD-Karte nicht beschreibbar.", 1000).show();
			return;
		}
		
		File cache=null;

		if(Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO)
		{
			cache=new File(Environment.getExternalStorageDirectory().getPath()+"/Android/data/de.xXephis.MeineMensa/cache/cachefu/image_cache/");
		}else{
			cache=new File(getExternalCacheDir().getAbsolutePath()+"/cachefu/image_cache/");
		}
		
		File toDelete[] = cache.listFiles(new FilenameFilter() {
			public boolean accept(File dir, String filename) {
				if(filename.startsWith("http"))
					return true;
				return false;
			}
		});
		boolean errorOcc=false;
		for(File target: toDelete)
		{
			if(!target.delete())
			{
				Log.e("cacheClearer", "File "+target.getName()+" not deleted!");
				errorOcc=true;
			}
		}
		
		if(!errorOcc)
		{
			Log.d("cacheClearer", "Files on SD deleted!");
		}
	}
	
	private boolean isSDWritable()
	{
		boolean mExternalStorageAvailable = false;
		boolean mExternalStorageWriteable = false;
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
		    // We can read and write the media
		    mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
		    // We can only read the media
		    mExternalStorageAvailable = true;
		    mExternalStorageWriteable = false;
		} else {
		    // Something else is wrong. It may be one of many other states, but all we need
		    //  to know is we can neither read nor write
		    mExternalStorageAvailable = mExternalStorageWriteable = false;
		}
		
		return (mExternalStorageAvailable && mExternalStorageWriteable);
	}
}
